package com.good.msgsys.service.impl;

import com.good.msgsys.dao.ProjectDao;
import com.good.msgsys.dto.PageResult;
import com.good.msgsys.entity.Project;
import com.good.msgsys.service.ProjectService;
import com.good.msgsys.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

/**
 * @author junlong
 * @date 2021/1/4 16:56
 */
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(Project project) {
        String id = idWorker.nextId() + "";
        project.setId(id);
        project.setState(0); // 默认保存（新增项目时，项目的状态为0）
        projectDao.save(project);
    }

    @Override
    public void deleteById(String id) {
        Project temp = projectDao.findById(id).get();
        temp.setState(6); // state: 6 - 项目已删除
        projectDao.save(temp);
    }

    @Override
    public void update(Project project) {
        Project temp = projectDao.findById(project.getId()).get();
        if (project.getNum() != null) temp.setNum(project.getNum());
        if (project.getName() != null) temp.setName(project.getName());
        if (project.getFroms() != null) temp.setFroms(project.getFroms());
        if (project.getApplicantDate() != null) temp.setApplicantDate(project.getApplicantDate());
        if (project.getType() != null) temp.setType(project.getType());
        if (project.getBudget() != null) temp.setBudget(project.getBudget());
        if (project.getGroups() != null) temp.setGroups(project.getGroups());
        projectDao.save(temp);
    }

    @Override
    public Project findById(String id) {
        return projectDao.findById(id).get();
    }

    @Override
    public PageResult<Project> findAll(Integer page, Integer limit, Integer state) {
        Specification<Project> spec = new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate cb1;
                if (state != null) {
                    cb1 = criteriaBuilder.equal(root.get("state"), state); // 项目状态：0未提交立项申请，1已提交，待立项审核，2已立项，3已提交结项申请，待审核，4项目通过，5项目不通过，6已删除
                } else {
                    cb1 = criteriaBuilder.notEqual(root.get("state"), 6); // 不查询已经删除的项目
                }
                return cb1;
            }
        };
        Pageable pageable = PageRequest.of(page, limit);
        Page<Project> projectPage = projectDao.findAll(spec, pageable);
        return new PageResult<>(projectPage.getTotalElements(), projectPage.getContent(), (long) projectPage.getTotalPages());
    }

    @Override
    public PageResult<Project> findAll(String userId, Integer page, Integer limit, Integer state) {
        Specification<Project> spec = new Specification<Project>() {
            @Override
            public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate cb1;
                if (state != null) {
                    cb1 = criteriaBuilder.equal(root.get("state"), state); // 项目状态：0未提交立项申请，1已提交，待立项审核，2已立项，3已提交结项申请，待审核，4项目通过，5项目不通过，6已删除
                } else {
                    cb1 = criteriaBuilder.notEqual(root.get("state"), 6); // 不查询已经删除的项目
                }
                Predicate cb2 = criteriaBuilder.equal(root.get("userId"), userId); // 查询当前userId的项目
                return criteriaBuilder.and(cb1, cb2);
            }
        };
        Pageable pageable = PageRequest.of(page, limit);
        Page<Project> projectPage = projectDao.findAll(spec, pageable);
        return new PageResult<>(projectPage.getTotalElements(), projectPage.getContent(), (long) projectPage.getTotalPages());
    }

    @Override
    public void apply(String projectId, Integer state) {
        Project temp = projectDao.findById(projectId).get();
        temp.setState(state);
        projectDao.save(temp);
    }

    @Override
    public void examine(String projectId, Integer state) {
        Project temp = projectDao.findById(projectId).get();
        temp.setState(state);
        projectDao.save(temp);
    }
}
